package com.mlick.a;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author lixiangxin
 * @date 2019/3/29 14:18
 **/
public class MainV2 {

    private static final int ALL_COUNT = 100;
    private int milkAllCount = ALL_COUNT * 2;
    private int fajiaojiAllCount = ALL_COUNT;

    private int carCount = 10;

    private AtomicInteger milkCount = new AtomicInteger();

    private AtomicInteger fajiaojiCount = new AtomicInteger();

    private AtomicInteger nailaoCount = new AtomicInteger();

    private AtomicInteger storeCount = new AtomicInteger();


    Object fajiao = new Object();
    Object milk = new Object();
    Object nailao = new Object();
    Object car = new Object();

    public static void main(String[] args) {

        MainV2 main = new MainV2();

        main.produceNaiLao();

        main.carTransForm();
    }


    private void carTransForm() {

        new Thread(() -> {
            while (true) {
                try {
                    TimeUnit.MILLISECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (storeCount.get() > carCount) {
                    System.out.println("车辆运输=====>" + storeCount.get());
                    storeCount.getAndAdd(-carCount);
                }
                if (nailaoCount.get() >= ALL_COUNT) {
                    System.out.println("=====运输完毕=====");
                    System.exit(-1);
                }
            }
        }).start();
    }

    private void produceNaiLao() {

        // 生产牛奶
        new Thread(() -> {
            while (milkAllCount-- > 0) {
                synchronized (milk){

                    nailao.notifyAll();
                    try {
                        TimeUnit.MILLISECONDS.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        }).start();

        // 生产发酵剂
        new Thread(() -> {
            while (true) {

                try {
                    TimeUnit.MILLISECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                fajiaojiCount.addAndGet(1);
                fajiaojiAllCount++;
                if (fajiaojiAllCount >= ALL_COUNT) {
                    System.out.println("=====发酵剂生产完毕=====");
                    break;
                }
                System.out.println("生产发酵剂=====>" + fajiaojiCount.get());

            }
        }).start();

        // 生产奶酪
        new Thread(() -> {
            while (true) {
                try {
                    TimeUnit.MILLISECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                if (milkCount.get() >= 2 && fajiaojiCount.get() >= 1 && storeCount.get() <= 1000) {
                    milkCount.addAndGet(-2);
                    fajiaojiCount.addAndGet(-1);
                    nailaoCount.addAndGet(1);
                    storeCount.addAndGet(1);
                    System.out.println("生产奶酪=====>" + nailaoCount.get());
                }

                if (nailaoCount.get() >= ALL_COUNT) {
                    System.out.println("=====奶酪生产完毕=====");
                    break;
                }
            }
        }).start();

    }


}
